package com.woniuxy.patrol.controller;

import com.woniuxy.common.utils.ResponseUtil;
import com.woniuxy.patrol.pojo.Community;
import com.woniuxy.patrol.service.CommunityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/community")
public class CommunityController {

    @Resource
    private CommunityService communityService;

    /**
     * 获取所有有效社区列表
     */
    @GetMapping("/list")
    public ResponseUtil getCommunityList() {
        List<Community> communityList = communityService.getAllCommunities();
        return ResponseUtil.success(communityList);
    }

    /**
     * 根据ID获取社区
     */
    @GetMapping("/{id}")
    public ResponseUtil getCommunityById(@PathVariable Long id) {
        Community community = communityService.getCommunityById(id);
        if (community != null && community.getIsDelete() == 1) {
            return ResponseUtil.success(community);
        }
        return ResponseUtil.error("社区不存在");
    }

    /**
     * 添加社区
     */
    @PostMapping("/add")
    public ResponseUtil addCommunity(@RequestBody Community community) {
        return communityService.addCommunity(community);
    }

    /**
     * 更新社区
     */
    @PostMapping("/update")
    public ResponseUtil updateCommunity(@RequestBody Community community) {
        return communityService.updateCommunity(community);
    }

    /**
     * 删除社区
     */
    @GetMapping("/delete/{id}")
    public ResponseUtil deleteCommunity(@PathVariable Long id) {
        return communityService.deleteCommunity(id);
    }
}